﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNHK.BLL;
using System.Threading;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;
using QLNHK.DANHMUC;
using System.Drawing.Imaging;
using System.IO;

namespace QLNHK
{
    public partial class FrmThemMoiDoiTuong : Form
    {
        private DoiTuong _objDoiTuong;
        public DoiTuong ObjDoiTuong
        {
            get { return _objDoiTuong; }
            set { _objDoiTuong = value; }
        }
        frmWait frmWaiting;
        private DoiTuong_TienAn _objTienAn;

        #region Set Authentication On Form
        private cls_Role ObjRole = null;
        private Cls_PHANQUYEN_NHOM ObjPhanQuyen = null;
        private Infragistics.Win.DefaultableBoolean GetStatus(bool values)
        {
            if (values)
            {
                return Infragistics.Win.DefaultableBoolean.True;
            }
            else
            {
                return Infragistics.Win.DefaultableBoolean.False;
            }
        }
        private void SetAuthenticationOnForm()
        {
            try
            {
                if (!frmMain.ObjNguoiDung.IsAdministrator)
                {
                    ObjRole = new cls_Role();
                    ObjPhanQuyen = new Cls_PHANQUYEN_NHOM();

                    ObjRole = new cls_Role(Global.GetRole(ObjPhanQuyen.GetRoleByIDChucNang(frmMain.ObjNguoiDung.ID_NguoiDung, this.Tag.ToString())));

                    if (ObjRole != null)
                    {
                        if (ObjRole.HienThi == false)//không hiển thị
                        {
                            foreach (Control ctl in this.Controls)
                            {
                                ctl.Visible = false;
                            }
                        }
                        //chỉ cho phép hiển thị, không cho phép cập nhật, sửa, xóa
                        else if (ObjRole.HienThi == true && ObjRole.Them == false && ObjRole.Sua == false && ObjRole.Xoa == false)
                        {
                            btnLuu.Enabled = btnAddAnh.Enabled = btnRemoveAnh.Enabled = (ObjRole.Them || ObjRole.Sua);
                        }
                        //cho phép hiển thị, cho phép hoặc cập nhật, hoặc sửa, hoặc xóa
                        else
                        {
                            CheckAuthentication();
                        }
                    }
                }

                btnDong.Enabled = true;
            }

            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
            }
        }
        private void CheckAuthentication()
        {
            try
            {
                if (ObjRole != null)
                {
                    btnLuu.Enabled = btnAddAnh.Enabled = btnRemoveAnh.Enabled = (ObjRole.Them || ObjRole.Sua);
                }
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
            }
        }
        #endregion

        public FrmThemMoiDoiTuong()
        {
            InitializeComponent();
        }
        private void FrmChuyenDoiTuong_Load(object sender, EventArgs e)
        {
            frmWaiting = new frmWait();
            ThreadPool.QueueUserWorkItem(OnProcess);
            frmWaiting.SetCaption("Vui lòng chờ giây lát");
            frmWaiting.ShowDialog(this);
        }
        private void OnProcess(object o)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    if (ObjDoiTuong == null)
                        //Thiet lap du lieu mac dinh
                        ResetData(ref frmWaiting);
                    else
                    {
                        ResetData(ref frmWaiting);

                        //Nap du lieu
                        LoadData(ref frmWaiting);
                    }

                    frmWaiting.DialogResult = DialogResult.OK;
                    Show();
                }));
            }
        }

        private void ResetData(ref frmWait frmW)
        {
            try
            {
                frmW.SetCaption("Đang tạo dữ liệu mặc định...");

                errorProvider1.Clear();

                txtHoLot.Text = txtTen.Text = txtTenGoiKhac.Text = txtNoiSinh.Text = string.Empty;
                Global.setDataToComboNgaySinh(cboNgaySinh);
                Global.setDataToComboThangSinh(cboThangSinh);
                Global.setDataToComboNamSinh(cboNamSinh);
                optGioiTinh.Value = 0; //0: Nu; 1: Nam; 2: Khong xac dinh

                //Que quan
                Global.setDataToComboThanhPho(cboThanhPhoQueQuan); cboThanhPhoQueQuan.Value = ConfigNHK.Install.ThanhPho; // Global.DIABAN_THANHPHO;
                Global.setDataToComboQuanHuyen(cboQuanHuyenQueQuan, (int)cboThanhPhoQueQuan.Value);
                Global.setDataToComboXaPhuong(cboXaPhuongQueQuan, (int)cboQuanHuyenQueQuan.Value);
                Global.setDataToComboToDanPho(cboToDanPhoQueQuan, (int)cboXaPhuongQueQuan.Value);

                Global.setDataToComboDanhMuc(cboDanToc, DMCatalogue.DM_DANTOC);
                Global.setDataToComboDanhMuc(cboTonGiao, DMCatalogue.DM_TONGIAO);
                Global.setDataToComboDanhMuc(cboQuocTich, DMCatalogue.DM_QUOCTICH);
                txtCMNDSo.Text = txtCMNDNoiCap.Text = string.Empty; dtCMNDNgayCap.DateTime = System.DateTime.Today;
                txtHoChieuSo.Text = string.Empty; dtHoChieuNgayCap.Value = dtHoChieuNgayHetHan.Value = System.DBNull.Value;

                //Thuong tru
                Global.setDataToComboThanhPho(cboThanhPhoThuongTru); cboThanhPhoThuongTru.Value = ConfigNHK.Install.ThanhPho; // Global.DIABAN_THANHPHO;
                Global.setDataToComboQuanHuyen(cboQuanHuyenThuongTru, (int)cboThanhPhoThuongTru.Value);
                Global.setDataToComboXaPhuong(cboXaPhuongThuongTru, (int)cboQuanHuyenThuongTru.Value);
                Global.setDataToComboToDanPho(cboToDanPhoThuongTru, (int)cboXaPhuongThuongTru.Value);
                txtDiaChiThuongTru.Text = string.Empty;

                //Noi o hien nay
                Global.setDataToComboThanhPho(cboThanhPhoHienNay); cboThanhPhoHienNay.Value = ConfigNHK.Install.ThanhPho; // Global.DIABAN_THANHPHO;
                Global.setDataToComboQuanHuyen(cboQuanHuyenHienNay, (int)cboThanhPhoHienNay.Value);
                Global.setDataToComboXaPhuong(cboXaPhuongHienNay, (int)cboQuanHuyenHienNay.Value);
                Global.setDataToComboToDanPho(cboToDanPhoHienNay, (int)cboXaPhuongHienNay.Value);
                txtDiaChiHienNay.Text = string.Empty;

                Global.setDataToComboDanhMuc(cboTrinhDoHocVan, DMCatalogue.DM_TRINHDO_HOCVAN);
                Global.setDataToComboDanhMuc(cboTrinhDoChuyenMon, DMCatalogue.DM_TRINHDO_CHUYENMON);
                //Global.setDataToComboDanhMuc(cboTrinhDoNgoaiNgu, DMCatalogue.DM_TRINHDO_NGOAINGU);
                //Global.setDataToComboDanhMuc(cboTiengDanToc, DMCatalogue.DM_DANTOC);
                txtNgheNghiep.Text = txtDienThoai.Text = txtTienAn.Text = string.Empty;

                dtCMNDNgayCap.Value = dtHoChieuNgayCap.Value = dtHoChieuNgayHetHan.Value = System.DBNull.Value;

                txtMaDoiTuong.Text = string.Empty;
                txtMaDoiTuong.ReadOnly = false;
                txtMaDoiTuong.Focus();

                //Load du lieu mac dinh cho grid
                LoadDataAnh(0);

                //Thong tin bo, me, vo/ chong lien quan
                txtHotenBo.Text = txtHoTenMe.Text = txtHoTenVoHoacChong.Text = txtQuanHeCanChuY.Text = string.Empty;
                Global.setDataToComboNamSinh(cboNamSinhBo);
                Global.setDataToComboNamSinh(cboNamSinhMe);
                Global.setDataToComboNamSinh(cboNamSinhVoHoacChong);

                //Dac diem ban than
                txtCao.Text = txtTamVoc.Text = txtGiongNoi.Text = txtMauDa.Text = txtNepTai.Text = txtDaiTai.Text = txtSongMui.Text = txtDauVet.Text = string.Empty;

                //Nghiep vu
                txtTienAn.Text = txtTienSu.Text = txtThuDoan.Text = txtDieuKien.Text = string.Empty;
                Global.setDataToComboDanhMuc(cboDanhMuc, DMCatalogue.DM_DANHMUCDOITUONG);
                Global.setDataToComboDanhMuc(cboHe, DMCatalogue.DM_HEDOITUONG);
                Global.setDataToComboDanhMuc(cboPhanLoai, DMCatalogue.DM_LOAIDOITUONG);
                txtThoiGianPhanLoai.Text = txtNoiLapHoSo.Text = txtSoDangKy.Text = string.Empty;
                dtNgayDangKy.Value = dtNgayKetThuc.Value = System.DBNull.Value;
                chkKetThucQuanLy.Checked = false;

                tabMain.Tabs[0].Selected = true;
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
        private void LoadData(ref frmWait frmW)
        {
            try
            {
                if (ObjDoiTuong == null) return;

                frmW.SetCaption("Đang mở dữ liệu đối tượng...");

                txtHoLot.Text = ObjDoiTuong.HoLot;
                txtTen.Text = ObjDoiTuong.Ten;
                txtTenGoiKhac.Text = ObjDoiTuong.TenGoiKhac;
                txtNoiSinh.Text = ObjDoiTuong.NoiSinh;
                cboNgaySinh.Value = ObjDoiTuong.NgaySinh;
                cboThangSinh.Value = ObjDoiTuong.ThangSinh;
                cboNamSinh.Value = ObjDoiTuong.NamSinh;
                optGioiTinh.Value = ObjDoiTuong.GioiTinh;

                //Que quan
                Global.setDataToComboThanhPho(cboThanhPhoQueQuan);
                cboThanhPhoQueQuan.Value = ObjDoiTuong.QueQuanTinhTPID;
                cboQuanHuyenQueQuan.Value = ObjDoiTuong.QueQuanQuanHuyenID;
                cboXaPhuongQueQuan.Value = ObjDoiTuong.QueQuanXaPhuongID;
                cboToDanPhoQueQuan.Value = ObjDoiTuong.QueQuanToID;

                cboDanToc.Value = ObjDoiTuong.DanTocID;
                cboTonGiao.Value = ObjDoiTuong.TonGiaoID;
                cboQuocTich.Value = ObjDoiTuong.QuocTichID;

                txtCMNDSo.Text = ObjDoiTuong.SoCMND;
                txtCMNDNoiCap.Text = ObjDoiTuong.NoiCapCMND;
                if (ObjDoiTuong.NgayCapCMND.Year > 1753)
                    dtCMNDNgayCap.DateTime = ObjDoiTuong.NgayCapCMND;

                //Thuong tru
                Global.setDataToComboThanhPho(cboThanhPhoThuongTru);
                cboThanhPhoThuongTru.Value = ObjDoiTuong.ThuongTruTinhTPID;
                cboQuanHuyenThuongTru.Value = ObjDoiTuong.ThuongTruQuanHuyenID;
                cboXaPhuongThuongTru.Value = ObjDoiTuong.ThuongTruXaPhuongID;
                cboToDanPhoThuongTru.Value = ObjDoiTuong.ThuongTruToID;
                txtDiaChiThuongTru.Text = ObjDoiTuong.ThuongTruDiaChi;

                //Cho o hien nay
                Global.setDataToComboThanhPho(cboThanhPhoHienNay);
                cboThanhPhoHienNay.Value = ObjDoiTuong.NoiOHienNayTinhTPID;
                cboQuanHuyenHienNay.Value = ObjDoiTuong.NoiOHienNayQuanHuyenID;
                cboXaPhuongHienNay.Value = ObjDoiTuong.NoiOHienNayXaPhuongID;
                cboToDanPhoHienNay.Value = ObjDoiTuong.NoiOHienNayToID;
                txtDiaChiHienNay.Text = ObjDoiTuong.NoiOHienNayDiaChi;

                cboTrinhDoHocVan.Value = ObjDoiTuong.TrinhDoHocVanID;
                cboTrinhDoChuyenMon.Value = ObjDoiTuong.TrinhDoChuyenMonID;
                txtNgheNghiep.Text = ObjDoiTuong.NgheNghiepNoiLamViec;
                txtDienThoai.Text = ObjDoiTuong.SoDienThoaiLienHe;

                txtMaDoiTuong.Text = ObjDoiTuong.MaDoiTuong;
                if (ObjDoiTuong.ID > 0)
                    txtMaDoiTuong.ReadOnly = true;
                txtHoLot.Focus();

                //Thong tin bo, me, vo/ chong lien quan
                txtHotenBo.Text = ObjDoiTuong.HoTenBo;
                txtHoTenMe.Text = ObjDoiTuong.HoTenMe;
                txtHoTenVoHoacChong.Text = ObjDoiTuong.HoTenVoChong;
                txtQuanHeCanChuY.Text = ObjDoiTuong.QuanHeCanChuY;
                cboNamSinhBo.Value = ObjDoiTuong.NamSinhBo;
                cboNamSinhMe.Value = ObjDoiTuong.NamSinhMe;
                cboNamSinhVoHoacChong.Value = ObjDoiTuong.NamSinhVoChong;

                //Dac diem ban than
                txtCao.Text = ObjDoiTuong.Cao;
                txtTamVoc.Text = ObjDoiTuong.TamVoc;
                txtGiongNoi.Text = ObjDoiTuong.GiongNoi;
                txtMauDa.Text = ObjDoiTuong.MauDa;
                txtNepTai.Text = ObjDoiTuong.NepTai;
                txtDaiTai.Text = ObjDoiTuong.DaiTai;
                txtSongMui.Text = ObjDoiTuong.SongMui;
                txtDauVet.Text = ObjDoiTuong.DauVet;

                //Nghiep vu
                List<DoiTuong_TienAn> list = DoiTuong_TienAn.SelectCollectionBy_DoiTuongID(ObjDoiTuong.ID);
                if (list.Count > 0)
                    _objTienAn = list[0];

                if (_objTienAn != null)
                {
                    txtTienAn.Text = _objTienAn.TienAn;
                    txtTienSu.Text = _objTienAn.TienSu;
                    txtThuDoan.Text = _objTienAn.ThuDoan;
                    txtDieuKien.Text = _objTienAn.DieuKien;
                    cboDanhMuc.Value = _objTienAn.DoiTuongDanhMucID;
                    cboHe.Value = _objTienAn.DoiTuongHeID;
                    cboPhanLoai.Value = _objTienAn.DoiTuongPhanLoaiID;
                    txtThoiGianPhanLoai.Text = _objTienAn.ThoiGianPhanLoai;
                    txtNoiLapHoSo.Text = _objTienAn.NoiLapHoSo;
                    txtSoDangKy.Text = _objTienAn.SoDangKy;
                    if (_objTienAn.NgayDangKy.Year > 1753)
                        dtNgayDangKy.Value = _objTienAn.NgayDangKy;
                    if (_objTienAn.NgayKetThuc.Year > 1753)
                        dtNgayKetThuc.Value = _objTienAn.NgayKetThuc;
                    chkKetThucQuanLy.Checked = _objTienAn.KetThucQuanLy;
                    txtBienPhapQuanLy.Text = _objTienAn.BienPhapQuanLy;
                }

                frmW.SetCaption("Đang mở dữ liệu ảnh đối tượng...");
                LoadDataAnh(ObjDoiTuong.ID);

                //Kiem tra phan quyen sau khi nap du lieu
                SetAuthenticationOnForm();
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
        private bool GetData()
        {
            bool isValid = false;
            try
            {
                if (ObjDoiTuong == null)
                    ObjDoiTuong = new DoiTuong();

                if (!ValidateForm())
                    return (isValid = false);

                ObjDoiTuong.MaDoiTuong = txtMaDoiTuong.Text;
                ObjDoiTuong.HoLot = txtHoLot.Text.Trim();
                ObjDoiTuong.Ten = txtTen.Text.Trim();
                ObjDoiTuong.TenGoiKhac = txtTenGoiKhac.Text;

                ObjDoiTuong.NoiSinh = txtNoiSinh.Text;
                ObjDoiTuong.NgaySinh = System.Convert.ToInt32(cboNgaySinh.Value);
                ObjDoiTuong.ThangSinh = System.Convert.ToInt32(cboThangSinh.Value);
                ObjDoiTuong.NamSinh = System.Convert.ToInt32(cboNamSinh.Value);
                ObjDoiTuong.GioiTinh = System.Convert.ToInt32(optGioiTinh.Value);

                //Que quan
                ObjDoiTuong.QueQuanTinhTPID = System.Convert.ToInt32(cboThanhPhoQueQuan.Value);
                ObjDoiTuong.QueQuanQuanHuyenID = System.Convert.ToInt32(cboQuanHuyenQueQuan.Value);
                ObjDoiTuong.QueQuanXaPhuongID = System.Convert.ToInt32(cboXaPhuongQueQuan.Value);
                ObjDoiTuong.QueQuanToID = System.Convert.ToInt32(cboToDanPhoQueQuan.Value);

                ObjDoiTuong.DanTocID = System.Convert.ToInt32(cboDanToc.Value);
                ObjDoiTuong.TonGiaoID = System.Convert.ToInt32(cboTonGiao.Value);
                ObjDoiTuong.QuocTichID = System.Convert.ToInt32(cboQuocTich.Value);

                ObjDoiTuong.SoCMND = txtCMNDSo.Text;
                ObjDoiTuong.NoiCapCMND = txtCMNDNoiCap.Text;
                ObjDoiTuong.NgayCapCMND = dtCMNDNgayCap.DateTime.Date;

                //Thuong tru
                ObjDoiTuong.ThuongTruTinhTPID = System.Convert.ToInt32(cboThanhPhoThuongTru.Value);
                ObjDoiTuong.ThuongTruQuanHuyenID = System.Convert.ToInt32(cboQuanHuyenThuongTru.Value);
                ObjDoiTuong.ThuongTruXaPhuongID = System.Convert.ToInt32(cboXaPhuongThuongTru.Value);
                ObjDoiTuong.ThuongTruToID = System.Convert.ToInt32(cboToDanPhoThuongTru.Value);
                ObjDoiTuong.ThuongTruDiaChi = txtDiaChiThuongTru.Text;

                //Cho o hien nay
                ObjDoiTuong.NoiOHienNayTinhTPID = System.Convert.ToInt32(cboThanhPhoHienNay.Value);
                ObjDoiTuong.NoiOHienNayQuanHuyenID = System.Convert.ToInt32(cboQuanHuyenHienNay.Value);
                ObjDoiTuong.NoiOHienNayXaPhuongID = System.Convert.ToInt32(cboXaPhuongHienNay.Value);
                ObjDoiTuong.NoiOHienNayToID = System.Convert.ToInt32(cboToDanPhoHienNay.Value);
                ObjDoiTuong.NoiOHienNayDiaChi = txtDiaChiHienNay.Text;

                ObjDoiTuong.TrinhDoHocVanID = System.Convert.ToInt32(cboTrinhDoHocVan.Value);
                ObjDoiTuong.TrinhDoChuyenMonID = System.Convert.ToInt32(cboTrinhDoChuyenMon.Value);
                ObjDoiTuong.NgheNghiepNoiLamViec = txtNgheNghiep.Text;
                ObjDoiTuong.SoDienThoaiLienHe = txtDienThoai.Text;

                //Thong tin bo, me, vo/ chong lien quan
                ObjDoiTuong.HoTenBo = txtHotenBo.Text;
                ObjDoiTuong.HoTenMe = txtHoTenMe.Text;
                ObjDoiTuong.HoTenVoChong = txtHoTenVoHoacChong.Text;
                ObjDoiTuong.QuanHeCanChuY = txtQuanHeCanChuY.Text;
                ObjDoiTuong.NamSinhBo = System.Convert.ToInt32(cboNamSinhBo.Value);
                ObjDoiTuong.NamSinhMe = System.Convert.ToInt32(cboNamSinhMe.Value);
                ObjDoiTuong.NamSinhVoChong = System.Convert.ToInt32(cboNamSinhVoHoacChong.Value);

                //Dac diem ban than
                ObjDoiTuong.Cao = txtCao.Text;
                ObjDoiTuong.TamVoc = txtTamVoc.Text;
                ObjDoiTuong.GiongNoi = txtGiongNoi.Text;
                ObjDoiTuong.MauDa = txtMauDa.Text;
                ObjDoiTuong.NepTai = txtNepTai.Text;
                ObjDoiTuong.DaiTai = txtDaiTai.Text;
                ObjDoiTuong.SongMui = txtSongMui.Text;
                ObjDoiTuong.DauVet = txtDauVet.Text;

                //Nghiep vu
                if (_objTienAn == null)
                    _objTienAn = new DoiTuong_TienAn();
                _objTienAn.TienAn = txtTienAn.Text;
                _objTienAn.TienSu = txtTienSu.Text;
                _objTienAn.ThuDoan = txtThuDoan.Text;
                _objTienAn.DieuKien = txtDieuKien.Text;
                _objTienAn.DoiTuongDanhMucID = System.Convert.ToInt32(cboDanhMuc.Value);
                _objTienAn.DoiTuongHeID = System.Convert.ToInt32(cboHe.Value);
                _objTienAn.DoiTuongPhanLoaiID = System.Convert.ToInt32(cboPhanLoai.Value);
                _objTienAn.ThoiGianPhanLoai = txtThoiGianPhanLoai.Text;
                _objTienAn.NoiLapHoSo = txtNoiLapHoSo.Text;
                _objTienAn.SoDangKy = txtSoDangKy.Text;
                if (dtNgayDangKy.Value != System.DBNull.Value && dtNgayDangKy.DateTime.Year > 1753)
                    _objTienAn.NgayDangKy = dtNgayDangKy.DateTime;
                if (dtNgayKetThuc.Value != System.DBNull.Value && dtNgayKetThuc.DateTime.Year > 1753)
                    _objTienAn.NgayKetThuc = dtNgayKetThuc.DateTime;
                _objTienAn.KetThucQuanLy = chkKetThucQuanLy.Checked;
                _objTienAn.BienPhapQuanLy = txtBienPhapQuanLy.Text;

                ObjDoiTuong.CanBoQuanLyID = frmMain.ObjNguoiDung.ID_NguoiDung;

                isValid = true;
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }

            return isValid;
        }
        private bool ValidateForm()
        {
            bool isValid = true;
            errorProvider1.Clear();

            try
            {
                //isValid &= Validations_Infragistics.ValidateChoose(cboTrinhDoChuyenMon, errorProvider1, "Trình độ chuyên môn");
                //isValid &= Validations_Infragistics.ValidateChoose(cboTrinhDoHocVan, errorProvider1, "Trình độ học vấn");

                isValid &= Validations_Infragistics.ValidateNull(txtDiaChiHienNay, errorProvider1, "Địa chỉ chỗ ở hiện nay");
                isValid &= Validations_Infragistics.ValidateChoose(cboQuanHuyenHienNay, errorProvider1, "Quận/ Huyện chỗ ở hiện nay");
                isValid &= Validations_Infragistics.ValidateChoose(cboThanhPhoHienNay, errorProvider1, "Tỉnh/ Thành phố chỗ ở hiện nay");

                isValid &= Validations_Infragistics.ValidateNull(txtDiaChiThuongTru, errorProvider1, "Địa chỉ nơi thường trú");
                isValid &= Validations_Infragistics.ValidateChoose(cboQuanHuyenThuongTru, errorProvider1, "Quận/ Huyện nơi thường trú");
                isValid &= Validations_Infragistics.ValidateChoose(cboThanhPhoThuongTru, errorProvider1, "Tỉnh/ Thành phố nơi thường trú");

                // isValid &= Validations_Infragistics.ValidateNull(dtCMNDNgayCap, errorProvider1, "Ngày cấp số CMND");
                //isValid &= Validations_Infragistics.ValidateNull(txtCMNDNoiCap, errorProvider1, "Nơi cấp số CMND");
                //isValid &= Validations_Infragistics.ValidateNull(txtCMNDSo, errorProvider1, "Số CMND");

                isValid &= Validations_Infragistics.ValidateChoose(cboQuocTich, errorProvider1, "Quốc tịch");
                isValid &= Validations_Infragistics.ValidateChoose(cboDanToc, errorProvider1, "Dân tộc");

                isValid &= Validations_Infragistics.ValidateChoose(cboQuanHuyenQueQuan, errorProvider1, "Quận/ Huyện quê quán");
                isValid &= Validations_Infragistics.ValidateChoose(cboThanhPhoQueQuan, errorProvider1, "Tỉnh/ Thành phố quê quán");

                isValid &= Validations_Infragistics.ValidateNull(txtNoiSinh, errorProvider1, "Nơi sinh");
                //Nam sinh
                string msgErr = "Chưa chọn thông tin '" + "Năm sinh" + "'.";

                errorProvider1.SetError(cboNamSinh, "");
                if (cboNamSinh.Text.Trim().Length == 0
                    || (cboNamSinh.SelectedIndex == 0 && cboNamSinh.Text.Contains("-")))
                {
                    isValid = false;
                    errorProvider1.SetError(cboNamSinh, msgErr);
                    cboNamSinh.Focus();
                }
                isValid &= Validations_Infragistics.ValidateChoose(cboThangSinh, errorProvider1, "Tháng sinh");
                isValid &= Validations_Infragistics.ValidateChoose(cboNgaySinh, errorProvider1, "Ngày sinh");
                isValid &= Validations_Infragistics.ValidateNull(txtTen, errorProvider1, "Tên");
                isValid &= Validations_Infragistics.ValidateNull(txtHoLot, errorProvider1, "Họ lót");

                isValid &= Validations_Infragistics.ValidateNull(txtMaDoiTuong, errorProvider1, "Mã đối tượng");

                if (isValid)
                {
                    bool isDouble = false;
                    string where = "MaDoiTuong = " + txtMaDoiTuong.Text.Trim();
                    //Kiem tra trung ma doi tuong
                    //New
                    if (ObjDoiTuong != null && ObjDoiTuong.ID == 0)
                        isDouble = DoiTuong.SelectCollectionDynamic(where, "").Count > 0;
                    //Edit
                    else if (ObjDoiTuong != null && ObjDoiTuong.ID != 0)
                    {
                        if (ObjDoiTuong.MaDoiTuong.ToString() != txtMaDoiTuong.Text.Trim())
                            isDouble = BLL.DoiTuong.SelectCollectionDynamic(where, "").Count > 0;
                        else
                            isDouble = BLL.DoiTuong.SelectCollectionDynamic(where, "").Count > 1;
                    }

                    if (isDouble)
                    {
                        MessageBox.Show("Đã có Mã đối tượng '" + txtMaDoiTuong.Text.Trim() + "' này trong danh sách.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtMaDoiTuong.Focus();
                        isValid = false;
                    }
                }
                if (isValid)
                {
                    if (txtHoLot.Text.Trim().Length > 0)
                    {
                        //Huy: Thêm vào để check trường hợp edit Doi tuong
                        bool isCheck = false;
                        if (ObjDoiTuong != null && (ObjDoiTuong.Ten != txtTen.Text || ObjDoiTuong.HoLot != txtHoLot.Text))
                            isCheck = true;
                        if (isCheck)
                        {
                            string where = "HoLot = N'" + txtHoLot.Text.Trim() + "' AND Ten = N'" + txtTen.Text.Trim() + "'";
                            //Kiem tra co trung ten nhan khau trong cung 1 ho khau
                            if (NhanKhau.SelectCollectionDynamic(where, "").Count > 0)
                            {
                                MessageBox.Show("Đã có Tên đối tượng '" + txtHoLot.Text.Trim() + " " + txtTen.Text.Trim() + "' này trong danh sách.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                txtHoLot.Focus();
                                isValid = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }

            return isValid;
        }

        private void cboThanhPhoQueQuan_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPhoQueQuan.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPhoQueQuan.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyenQueQuan, Convert.ToInt32(cboThanhPhoQueQuan.Value));
            }
        }
        private void cboQuanHuyenQueQuan_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyenQueQuan.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyenQueQuan.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuongQueQuan, Convert.ToInt32(cboQuanHuyenQueQuan.Value));
            }
        }
        private void cboXaPhuongQueQuan_ValueChanged(object sender, EventArgs e)
        {
            if (cboXaPhuongQueQuan.Value != null)
            {
                int cboXaPhuongValue = 0;
                Int32.TryParse(cboXaPhuongQueQuan.Value.ToString(), out cboXaPhuongValue);
                if (cboXaPhuongValue > 0)
                    Global.setDataToComboToDanPho(cboToDanPhoQueQuan, Convert.ToInt32(cboXaPhuongQueQuan.Value));
            }
        }

        private void cboThanhPhoThuongTru_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPhoThuongTru.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPhoThuongTru.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyenThuongTru, Convert.ToInt32(cboThanhPhoThuongTru.Value));
            }
        }
        private void cboQuanHuyenThuongTru_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyenThuongTru.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyenThuongTru.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuongThuongTru, Convert.ToInt32(cboQuanHuyenThuongTru.Value));
            }
        }
        private void cboXaPhuongThuongTru_ValueChanged(object sender, EventArgs e)
        {
            if (cboXaPhuongThuongTru.Value != null)
            {
                int cboXaPhuongValue = 0;
                Int32.TryParse(cboXaPhuongThuongTru.Value.ToString(), out cboXaPhuongValue);
                if (cboXaPhuongValue > 0)
                    Global.setDataToComboToDanPho(cboToDanPhoThuongTru, Convert.ToInt32(cboXaPhuongThuongTru.Value));
            }
        }

        private void cboThanhPhoHienNay_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPhoHienNay.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPhoHienNay.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyenHienNay, Convert.ToInt32(cboThanhPhoHienNay.Value));
            }
        }
        private void cboQuanHuyenHienNay_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyenHienNay.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyenHienNay.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuongHienNay, Convert.ToInt32(cboQuanHuyenHienNay.Value));
            }
        }
        private void cboXaPhuongHienNay_ValueChanged(object sender, EventArgs e)
        {
            if (cboXaPhuongHienNay.Value != null)
            {
                int cboXaPhuongValue = 0;
                Int32.TryParse(cboXaPhuongHienNay.Value.ToString(), out cboXaPhuongValue);
                if (cboXaPhuongValue > 0)
                    Global.setDataToComboToDanPho(cboToDanPhoHienNay, Convert.ToInt32(cboXaPhuongHienNay.Value));
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                //Cap nhat gia tri
                if (!GetData())
                    return;

                if (ObjDoiTuong.ID > 0)
                    ObjDoiTuong.Update();
                else
                    ObjDoiTuong.Insert();

                //Luu anh
                SaveDataAnh(ObjDoiTuong.ID);

                MessageBox.Show("Đã lưu thành công", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                MessageBox.Show("Lỗi trong quá trình xử lý: " + ex.Message);
            }
            finally { Cursor = Cursors.Default; }
        }
        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            //Huy bo thong tin dieu chinh, tai lai thong tin cu truoc khi thay doi
            frmWaiting = new frmWait();
            ThreadPool.QueueUserWorkItem(OnProcess);
            frmWaiting.SetCaption("Vui lòng chờ giây lát");
            frmWaiting.ShowDialog(this);
        }

        #region IMAGE
        private DoiTuong_HinhAnh ObjDoiTuongAnh;
        private OpenFileDialog openFileDialog = new OpenFileDialog();
        private void btnAddAnh_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png)|*.jpg;*.jpeg;*.png";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FileName = string.Empty;

                if (DialogResult.OK == openFileDialog.ShowDialog())
                {
                    picAnh.Image = Image.FromFile(openFileDialog.FileName);

                    FileInfo fiAnh = new FileInfo(openFileDialog.FileName);

                    if (ObjDoiTuongAnh == null)
                        ObjDoiTuongAnh = new DoiTuong_HinhAnh();
                    ObjDoiTuongAnh.TenAnh = fiAnh.Name;
                    ObjDoiTuongAnh.Loai = 0;
                    ObjDoiTuongAnh.Anh = Global.ImageToByteArray((Image)picAnh.Image, (fiAnh.Extension.Contains("JPEG") || fiAnh.Extension.Contains("JPG") ? ImageFormat.Jpeg : ImageFormat.Png));
                    if (ObjDoiTuong != null && ObjDoiTuong.ID > 0)
                        ObjDoiTuongAnh.DoiTuongID = ObjDoiTuong.ID;
                }
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                MessageBox.Show("Không đúng định dạng ảnh", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void btnRemoveAnh_Click(object sender, EventArgs e)
        {
            try
            {
                picAnh.Image = null;
                if (ObjDoiTuongAnh != null)
                {
                    ObjDoiTuongAnh.Anh = null;
                    ObjDoiTuongAnh.TenAnh = string.Empty;
                }
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
        private void LoadDataAnh(long doiTuongID)
        {
            try
            {
                List<DoiTuong_HinhAnh> anhs = DoiTuong_HinhAnh.SelectCollectionBy_DoiTuongID(doiTuongID);
                if (anhs.Count > 0)
                    ObjDoiTuongAnh = anhs[0];

                if (ObjDoiTuongAnh != null && ObjDoiTuongAnh.Anh != null)
                    picAnh.Image = Global.ByteArrayToImage(ObjDoiTuongAnh.Anh);
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
        private void SaveDataAnh(long doiTuongID)
        {
            try
            {
                if (ObjDoiTuongAnh == null)
                    return;

                if (ObjDoiTuongAnh.ID > 0)
                    ObjDoiTuongAnh.Update();
                else
                {
                    ObjDoiTuongAnh.DoiTuongID = doiTuongID;
                    ObjDoiTuongAnh.Insert();
                }
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
        #endregion
    }
}